#!/usr/bin/env python
"""
Convert records from AMQP runs to
something a bit easier to process in R.

Expected input:
 message_id,worker_id,src_stage,dst_stage,rcv_ts,snd_ts
 1,Master,0,1,NA,1287515832.745467
"""
import sys

def run(f, header=True, run_num=0):
    rn = run_num
    if header:
        print("run,stage,ts,mid,wid")
    wid, saved = None, [ ]
    for lineno, line in enumerate(f):
        if line[0] == 'm': #hdr
            wid, saved = None, [ ] # reset worker
            rn += 1 # inc run num
            continue
        try:
            parts = m_id, w_id, sstg, dstg, rts, sts = line.split(',')
        except ValueError:
            input_error(lineno, line)
            return -1
        #print(parts)
        if wid is None:
            if w_id == 'Master':
                saved.append(parts)
                continue
            else:
                wid = w_id
                for parts in saved:
                    process('%d' % rn, parts[0], wid, *parts[2:])
                saved = [ ]
        elif w_id in ('Consumer', 'Master'):
            w_id = wid
        process('%d' % rn, m_id, w_id, sstg, dstg, rts, sts)
    return 0

def process(rn, m_id, w_id, sstg, dstg, rts, sts):
    sts = sts[:-1]
    if sstg == '0':
        ts1, ts2 = sts, sts
    elif dstg == 'NA':
        ts1, ts2 = rts, rts
    else:
        ts1, ts2 = rts, sts
    print(','.join((rn, sstg + '-B', ts1, m_id, w_id)))
    print(','.join((rn, sstg + '-E', ts2, m_id, w_id)))

def usage():
    import os
    print("""
usage: {prog} [files..]
    With no files, input is read from stdin.
    Output is to stdout.
""".format(prog=os.path.basename(sys.argv[0])))
    sys.exit(1)

def input_error(lineno, line):
    sys.stderr.write("ERROR: Bad input at line {n:d}: {line}".format(line=line, n=lineno))

if __name__ == '__main__':
    status = 0
    if len(sys.argv) > 1:
        if '-h' in sys.argv or '--help' in sys.argv:
            usage()
        hdr, rn = True, 0
        for name in sys.argv[1:]:
            infile = open(name, 'r')
            status = run(infile, header=hdr, run_num=rn)
            if status != 0: break
            hdr = False
            rn += 1
    else:
        status = run(sys.stdin)
    sys.exit(status)
